package com.youyi.yychosesdk.result;

import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import com.youyi.yychosesdk.constant.Type;
import com.youyi.yychosesdk.models.album.entity.Photo;
import com.youyi.yychosesdk.setting.Setting;
import java.util.ArrayList;
import java.util.Iterator;

public class Result {
    public static final int ADD_SUCCESS = 0;
    public static final int PICTURE_OUT = -1;
    public static final int SINGLE_TYPE = -3;
    public static final int VIDEO_OUT = -2;
    public static ArrayList<Photo> photos = new ArrayList<>();

    public static int addPhoto(Photo photo) {
        if (photos.isEmpty()) {
            photo.selected = true;
            photos.add(photo);
            return 0;
        }
        if (Setting.complexSelector) {
            if (Setting.complexSingleType) {
                if (photos.get(0).type.contains(Type.VIDEO) && !photo.type.contains(Type.VIDEO)) {
                    return -3;
                }
                if (!photos.get(0).type.contains(Type.VIDEO) && photo.type.contains(Type.VIDEO)) {
                    return -3;
                }
            }
            int videoNumber = getVideoNumber();
            if (photo.type.contains(Type.VIDEO) && videoNumber >= Setting.complexVideoCount) {
                return -2;
            }
            int size = photos.size() - videoNumber;
            if (!photo.type.contains(Type.VIDEO) && size >= Setting.complexPictureCount) {
                return -1;
            }
        }
        photo.selected = true;
        photos.add(photo);
        return 0;
    }

    public static void removePhoto(Photo photo) {
        photo.selected = false;
        photos.remove(photo);
    }

    public static void removePhoto(int i) {
        removePhoto(photos.get(i));
    }

    public static void removeAll() {
        int size = photos.size();
        for (int i = 0; i < size; i++) {
            removePhoto(0);
        }
    }

    private static int getVideoNumber() {
        Iterator<Photo> it = photos.iterator();
        int i = 0;
        while (it.hasNext()) {
            if (it.next().type.contains(Type.VIDEO)) {
                i++;
            }
        }
        return i;
    }

    public static void processOriginal() {
        boolean z = Build.VERSION.SDK_INT == 15;
        if (Setting.showOriginalMenu && Setting.originalMenuUsable) {
            Iterator<Photo> it = photos.iterator();
            while (it.hasNext()) {
                Photo next = it.next();
                next.selectedOriginal = Setting.selectedOriginal;
                if (z && next.width == 0) {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(next.path, options);
                    next.width = options.outWidth;
                    next.height = options.outHeight;
                }
            }
        }
    }

    public static void clear() {
        photos.clear();
    }

    public static boolean isEmpty() {
        return photos.isEmpty();
    }

    public static int count() {
        return photos.size();
    }

    public static String getSelectorNumber(Photo photo) {
        return String.valueOf(photos.indexOf(photo) + 1);
    }

    public static String getPhotoPath(int i) {
        return photos.get(i).path;
    }

    public static Uri getPhotoUri(int i) {
        return photos.get(i).uri;
    }

    public static String getPhotoType(int i) {
        return photos.get(i).type;
    }

    public static long getPhotoDuration(int i) {
        return photos.get(i).duration;
    }
}
